package com.fcscs.cservice.dao.jdbc;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.fcscs.cservice.dao.GuestCronJobDao;
import com.mchange.v2.c3p0.PooledDataSource;

@Repository("guestCronJobDaoJdbc")
public class GuestCronJobDaoJdbc implements GuestCronJobDao {
	
	private static Logger LOG = Logger.getLogger(GuestCronJobDaoJdbc.class);

	private SimpleJdbcTemplate templ;

	@Autowired
	protected GuestCronJobDaoJdbc(@Qualifier("dataSourceGuest") PooledDataSource ds) {
		templ = new SimpleJdbcTemplate(ds);
	}
	
	/* (non-Javadoc)
	 * @see com.fcscs.cservice.dao.jdbc.CronJobDao#atomicUpdateDateTime()
	 */
	@Override
	public void atomicBatchUpdate(Map<String, Object> params) {
		try {
			templ.update("update reservation set arrival_date_time = DATE_ADD(arrival_date_time, INTERVAL 1 DAY)", new HashMap<String, Object>());
			templ.update("update reservation set arrival_flight_date = DATE_ADD(arrival_flight_date, INTERVAL 1 DAY)", new HashMap<String, Object>());
			templ.update("update reservation set departure_date_time = DATE_ADD(departure_date_time, INTERVAL 1 DAY)", new HashMap<String, Object>());
			templ.update("update reservation set departure_flight_date = DATE_ADD(departure_flight_date, INTERVAL 1 DAY)", new HashMap<String, Object>());
			templ.update("update rate set from_date = DATE_ADD(from_date, INTERVAL 1 DAY)", new HashMap<String, Object>());
			templ.update("update rate set to_date = DATE_ADD(to_date, INTERVAL 1 DAY)", new HashMap<String, Object>());
			templ.update("update billing set billing_date_time = DATE_ADD(billing_date_time, INTERVAL 1 DAY)", new HashMap<String, Object>());
			templ.update("update booking set reservation_date = DATE_ADD(reservation_date, INTERVAL 1 DAY)", new HashMap<String, Object>());
			templ.update("update rc set status = 'NEW'", new HashMap<String, Object>());
		} catch (Exception e) {
			LOG.error("Error locking row", e);
		}
	}

}
